//Activity to set the day/night temperature

package g1.thermostat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class T2Activity extends Activity {

	ImageButton dtUp;
	ImageButton dtUp2;
	ImageButton dtDown;
	ImageButton dtDown2;
	ImageButton ntUp;
	ImageButton ntUp2;
	ImageButton ntDown;
	ImageButton ntDown2;
	Button btnCancel2;
	Button btnSave2;
	TextView dayTemp;
	TextView nightTemp;
	double vdt;
	double vnt;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout2);
		
		//double dTemp = ((Variables) this.getApplication()).getDayTemperature();
		//vdt = dTemp;
		vdt = ((Variables) this.getApplication()).getDayTemperature();
		vnt = ((Variables) this.getApplication()).getNightTemperature();;

		dtUp = (ImageButton) findViewById(R.id.dtUp);
		dtUp2 = (ImageButton) findViewById(R.id.dtUp2);
		dtDown = (ImageButton) findViewById(R.id.dtDown);
		dtDown2 = (ImageButton) findViewById(R.id.dtDown2);
		ntUp = (ImageButton) findViewById(R.id.ntUp);
		ntUp2 = (ImageButton) findViewById(R.id.ntUp2);
		ntDown = (ImageButton) findViewById(R.id.ntDown);
		ntDown2 = (ImageButton) findViewById(R.id.ntDown2);

		btnCancel2 = (Button) findViewById(R.id.btnCancel2);
		btnSave2 = (Button) findViewById(R.id.btnSave2);
		
		dayTemp = (TextView) findViewById(R.id.dayTemp);
		dayTemp.setText(Double.toString(vdt));
		
		nightTemp = (TextView) findViewById(R.id.nightTemp);
		nightTemp.setText(Double.toString(vnt));

		dtUp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vdt <= 29) {
					vdt++;

				} else if (vdt > 29 && vdt < 30) {
					vdt = 30;
				}
				if(vdt==30){
					dtUp.setEnabled(false);
					dtUp2.setEnabled(false);
				}
				dtDown.setEnabled(true);
				dtDown2.setEnabled(true);
				dayTemp.setText(Double.toString(vdt));
//				((Variables)getApplication()).setDayTemperature(vdt);
			}
		});
		//((Variables) this.getApplication()).setDayTemperature(vdt);

		dtUp2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vdt <= 29.9) {
					if (vdt == 29.9){
						dtUp.setEnabled(false);
						dtUp2.setEnabled(false);
					}
					vdt = vdt + 0.1;
					vdt = (double) Math.round(vdt * 10) / 10;

				}
				dtDown.setEnabled(true);
				dtDown2.setEnabled(true);
				dayTemp.setText(Double.toString(vdt));
//				((Variables)getApplication()).setDayTemperature(vdt);
			}
		});

		dtDown.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vdt >= 6) {
					vdt--;

				} else if (vdt < 6 && vdt > 5) {
					vdt = 5;
				}
				if(vdt==5){
					dtDown.setEnabled(false);
					dtDown2.setEnabled(false);
				}
				dtUp.setEnabled(true);
				dtUp2.setEnabled(true);
				dayTemp.setText(Double.toString(vdt));
//				((Variables)getApplication()).setDayTemperature(vdt);
			}
		});
		dtDown2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vdt >= 5.1) {
					if (vdt == 5.1){
						dtDown.setEnabled(false);
						dtDown2.setEnabled(false);
					}
					vdt = vdt - 0.1;
					vdt = (double) Math.round(vdt * 10) / 10;

				}
				dtUp.setEnabled(true);
				dtUp2.setEnabled(true);
				dayTemp.setText(Double.toString(vdt));
//				((Variables)getApplication()).setDayTemperature(vdt);
			}
		});
		ntUp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vnt <= 29) {
					vnt++;

				} else if (vnt > 29 && vnt < 30) {
					vnt = 30;
				}
				if(vnt==30){
					ntUp.setEnabled(false);
					ntUp2.setEnabled(false);
				}
				ntDown.setEnabled(true);
				ntDown2.setEnabled(true);
				nightTemp.setText(Double.toString(vnt));
//				((Variables)getApplication()).setNightTemperature(vnt);
			}
		});

		ntUp2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vnt <= 29.9) {
					if (vnt == 29.9){
						ntUp.setEnabled(false);
						ntUp2.setEnabled(false);
					}
					vnt = vnt + 0.1;
					vnt = (double) Math.round(vnt * 10) / 10;

				}
				ntDown.setEnabled(true);
				ntDown2.setEnabled(true);
				nightTemp.setText(Double.toString(vnt));
//				((Variables)getApplication()).setNightTemperature(vnt);
			}
		});

		ntDown.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vnt >= 6) {
					vnt--;

				} else if (vnt < 6 && vnt > 5) {
					vnt = 5;
				}
				if(vnt==5){
					ntDown.setEnabled(false);
					ntDown2.setEnabled(false);
				}
				ntUp.setEnabled(true);
				ntUp2.setEnabled(true);
				nightTemp.setText(Double.toString(vnt));
//				((Variables)getApplication()).setNightTemperature(vnt);
			}
		});
		ntDown2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vnt >= 5.1) {
					if (vnt == 5.1){
						ntDown.setEnabled(false);
						ntDown2.setEnabled(false);
					}
					vnt = vnt - 0.1;
					vnt = (double) Math.round(vnt * 10) / 10;

				}
				ntUp.setEnabled(true);
				ntUp2.setEnabled(true);
				nightTemp.setText(Double.toString(vnt));
//				((Variables)getApplication()).setNightTemperature(vnt);
			}
		});
		
		btnCancel2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				((Variables)getApplication()).setSave2(false);
//				Intent intent = new Intent();
//				intent.setClass(T2Activity.this, T1Activity.class);
//				startActivity(intent);
				finish();
			}	

		});
		
		btnSave2.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				((Variables)getApplication()).setSave2(true);
				((Variables)getApplication()).setDayTemperature(vdt);
				((Variables)getApplication()).setNightTemperature(vnt);
//		        ((Variables)getApplication()).setOvEnable(ovEnable);
//				Intent intent = new Intent();
//				intent.setClass(T2Activity.this, T1Activity.class);
//				startActivity(intent);
				finish();
			}	

		});

	}

}